package com.bdqn.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bdqn.demo.pojo.Resource;
import com.bdqn.demo.pojo.Role;
import com.bdqn.demo.service.RoleService;
import com.bdqn.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/findAll")
    public Result findAll() {
        List<Role> list = roleService.findAll();
        return Result.ok("true", list);
    }

    @PutMapping("/allocate")
    public Result allocate(Integer uid, Integer roleId) {
        boolean result = roleService.allocate(uid, roleId);
        return result ? Result.ok("修改成功") : Result.fail("操作失败");
    }

    @GetMapping("/getPage/{pageNo}/{pageSize}")
    public Result getPage(@PathVariable Integer pageNo, @PathVariable Integer pageSize, String name) {
        IPage<Role> page = roleService.getPage(pageNo, pageSize, name);
        return Result.ok("查询成功", page);
    }

    @PutMapping("/statusUpdate")
    public Result statusUpdate(Integer id, Integer status) {
        boolean result = roleService.update().set("status", status).eq("id", id).update();
        return result ? Result.ok("修改成功") : Result.fail("操作失败");
    }

    @PutMapping("/allocate_menu")
    public Result allocateMenu(Integer roleId, Integer[] menuIds) {
        boolean result = roleService.allocateMenu(roleId, menuIds);
        return result ? Result.ok("修改成功") : Result.fail("数据问题");
    }

    @PutMapping("/allocate_resource")
    public Result allocateResource(Integer roleId, Integer[] resourceIds) {
         boolean result = roleService.allocateResource(roleId, resourceIds);
        return result ? Result.ok("修改成功") : Result.fail("数据问题");
    }

    @DeleteMapping("/delete/{rid}")
    public Result delete(@PathVariable Integer rid){
        boolean result = roleService.removeById(rid);
        return result ? Result.ok("删除成功") : Result.fail("删除失败");
    }


}
